package com.chuangke.mds.dao.mapper.provider;

import io.lettuce.core.dynamic.annotation.Param;

/**
 * @author panyingzu
 * @title TechnologySimSqlProvider
 * @description
 * @Date 2025/10/1 23:59
 */
public class TechnologySimSqlProvider {
    public String getPage(@Param("processId") String processId, @Param("processName") String processName,@Param("procedureName") String procedureName,
                               @Param("productMx") String productMx,@Param("status")String status,@Param("start") String start, @Param("end") String end,@Param("userDeptId") String userDeptId) {
        StringBuilder sql = new StringBuilder();
        sql.append("select\n");
        sql.append("a.*,\n");
        sql.append("b.name process_name,\n");
        sql.append("d.name product_name,\n");
        sql.append("c.name procedure_name\n");
        sql.append("from\n");
        sql.append("mds_technology_process_procedure_node_simulate a\n");
        sql.append("left join mds_technology_process b on a.process_id = b.id\n");
        sql.append("left join mds_technology_process_procedure c on a.process_id = c.process_id and a.procedure_id = c.id\n");
        sql.append("left join mds_product d on b.product_id = d.id\n");
        sql.append("left join sys_dict e on c.type = e.code and e.type = 'mds_product_mx_type' \n");

        sql.append("where 1=1 AND a.name IS NOT NULL ");

        if (processId != null && !processId.trim().isEmpty()) {
            sql.append(" and b.id = #{processId}");
        }

        if (processName != null && !processName.trim().isEmpty()) {
            sql.append(" and b.name LIKE CONCAT('%', #{processName}, '%')");
        }

        if (procedureName != null && !procedureName.trim().isEmpty()) {
            sql.append(" and c.name LIKE CONCAT('%', #{procedureName}, '%')");
        }
        if (productMx != null && !productMx.trim().isEmpty() && !"all".equals(productMx)) {
            sql.append(" and e.ext_str01 LIKE CONCAT('%', #{productMx}, '%')");
        }


        if (status != null && !status.trim().isEmpty() && !"all".equals(status)) {
                sql.append(" and a.status = #{status}");
            }


        if (end != null && !end.trim().isEmpty()) {
            sql.append(" AND DATE_FORMAT(a.created_at,'%Y-%m-%d') <= #{end} ");
        }

        if (start != null && !start.trim().isEmpty()) {
            sql.append(" AND DATE_FORMAT(a.created_at,'%Y-%m-%d') >= #{start} ");
        }

        if (userDeptId != null && !userDeptId.trim().isEmpty()) {
            sql.append(" and b.dept_id = #{userDeptId}");
        }

        sql.append(" order by created_at desc");


        return sql.toString();
    }
}
